<%%
<% if db_adapter != :sqlite3 -%>
db_host    = ENV.fetch('DB_HOST', ENV.fetch('<%= app_name.upcase %>_DB_1_PORT_5432_TCP_ADDR', 'localhost'))
db_port    = ENV['<%= app_name.upcase %>_DB_1_PORT_5432_TCP_PORT'] || ENV['DB_PORT'] || <%= {postgresql: 5432, mysql2: 3306}[db_adapter] %>
<% end -%>
require 'etc'
pool_size =
  # Web: max workers * max threads
  ENV.fetch('WEB_CONCURRENCY', 3).to_i * ENV.fetch('MAX_THREADS', 5).to_i +
  # ActiveJob Async max thread pool size
  Etc.nprocessors
ERB
%>

defaults: &defaults
  adapter: <%= db_adapter %>
  min_messages: WARNING
  pool: <%%= pool_size %>
<% if db_adapter == :sqlite3 -%>
  timeout: 5000
<% else -%>
  host: <%%= db_host %>
  port: <%%= db_port %>
  username: <%%= ENV.fetch('DB_USERNAME', '<%= dev_user %>').inspect %>
  password: <%%= ENV.fetch('DB_PASSWORD', <%= dev_user_password.inspect %>).inspect %>
<% end -%>
<% if db_adapter == :mysql2 -%>
  encoding: utf8mb4
  charset: utf8mb4
  collation: utf8mb4_unicode_ci
<% else -%>
  encoding: utf8
<% end -%>

development:
  <<: *defaults
  database: <%= db_adapter == :sqlite3 ? 'db/development.sqlite3' : "#{app_name}_dev" %>

test:
  <<: *defaults
  database: <%= db_adapter == :sqlite3 ? 'db/test.sqlite3' : "#{app_name}_test" %>


production:
  <<: *defaults
  encoding: utf8
  min_messages: WARNING
  url: <%%= ENV['DATABASE_URL'].inspect if ENV['DATABASE_URL'] %>
  database: <%%= '<%= (db_adapter == :sqlite3 ? 'db/production.sqlite3' : "#{app_name}_prod") %>' unless ENV['DATABASE_URL'] %>
